home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Divestiture / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.8 KB  |  154 lines

  1. #include <Gestalt.h> 
  2. #include <MacTypes.h>
  3. #include <Resources.h>
  4. #include <Traps.h>
  5. #include <MixedMode.h>
  6. #include <Processes.h>
  7.  
  8. #include "tear.h"
  9. #include "wdef.h"
  10.  
  11. #include "main_i.h"
  12.  
  13. static    InitGrafUPP    gSaveInitGraf;
  14. static    ShowHideUPP    gSaveShowHide;
  15.  
  16. static    OSType    **gMSAppList;
  17. static    UInt32    gMSAppCount;
  18.  
  19. static    UniversalProcPtr ApplyTrapPatch (short trap, UniversalProcPtr patchPtr)
  20. {
  21.     UniversalProcPtr trapPtr;
  22.     
  23.     if (patchPtr == nil)
  24.         return nil;
  25.  
  26.     trapPtr = NGetTrapAddress (trap, (trap & 0x0800) ? ToolTrap : OSTrap);
  27.     NSetTrapAddress (patchPtr, trap, (trap & 0x0800) ? ToolTrap : OSTrap);
  28.     
  29.     return trapPtr;
  30. }
  31.  
  32. static    bool    HaveProcessManager(void)
  33. {
  34.     static    bool    alreadyKnowWeHaveIt = false;
  35.     SInt32            response;
  36.     OSErr            error;
  37.     
  38.     if (alreadyKnowWeHaveIt)
  39.         return true;
  40.  
  41.     error = Gestalt (gestaltOSAttr, &response);
  42.     
  43.     if ((error != noErr) || ((response & gestaltLaunchControl) == 0)) {
  44.         return false;
  45.     }
  46.     
  47.     alreadyKnowWeHaveIt = true;
  48.     
  49.     return true;
  50. }
  51.  
  52. static    bool    is_MS_app(OSType signature)
  53. {
  54.     UInt32    i;
  55.     OSType    *p;
  56.         
  57.     for (i = 0, p = *gMSAppList; i < gMSAppCount; i++, p++)
  58.     {
  59.         if (signature == *p)
  60.             return true;
  61.     }
  62.     
  63.     return false;
  64. }
  65.  
  66. static    bool    prepare_app_list(void)
  67. {
  68.     Handle    h;
  69.     
  70.     h = Get1Resource(kMSAppListRsrcType, 128);
  71.     if (h != nil)
  72.     {
  73.         UInt32    len = GetHandleSize(h);
  74.         
  75.         gMSAppCount = (len / sizeof(OSType));
  76.  
  77.         gMSAppList = (OSType**)(NewHandleSys(len));
  78.         if (gMSAppList != nil)
  79.         {
  80.             BlockMoveData(*h, *gMSAppList, len);
  81.             return (gMSAppCount > 0);
  82.         }
  83.     }
  84.     
  85.     return false;
  86. }
  87.  
  88. #pragma mark -
  89.  
  90. static    void    Patched_ShowHide(WindowRef w, Boolean showOrHide)
  91. {
  92.     //    slam the window's defproc handle to point to our stuff
  93.     //    ...if necessary.
  94.     
  95.     FixUpWDEF(w);
  96.  
  97.     CallShowHideProc(gSaveShowHide, w, showOrHide);
  98. }
  99.  
  100. static    void    Patched_InitGraf (void* ioGlobals)
  101. {
  102.     ProcessSerialNumber        currentProcess = { 0, kCurrentProcess };
  103.     ProcessInfoRec            processInfo;
  104.  
  105.     processInfo.processInfoLength = sizeof (processInfo);
  106.     processInfo.processAppSpec = nil;
  107.     processInfo.processName = nil;
  108.     
  109.     if (HaveProcessManager() &&
  110.         (GetProcessInformation (¤tProcess, &processInfo) == noErr) &&
  111.         is_MS_app(processInfo.processSignature))
  112.     {
  113.         gSaveShowHide = ApplyTrapPatch (_ShowHide, NewShowHideProc (Patched_ShowHide));
  114.     }
  115.     
  116.     CallInitGrafProc (gSaveInitGraf, ioGlobals);
  117. }
  118.  
  119. #pragma mark -
  120.  
  121. void main (void)
  122. {
  123.     Handle    initCode = nil;
  124.     THz        theZone;
  125.  
  126.     /* Detach and lock the code resource */
  127.         
  128.     initCode = Get1Resource ('INIT', 0);
  129.     if (initCode == nil)
  130.         goto failure;
  131.  
  132.     DetachResource (initCode);
  133.     if (ResError () != noErr) 
  134.         goto failure;
  135.     
  136.     if (PrepareTearRgn() != noErr)
  137.         goto failure;
  138.     
  139.     if (! prepare_app_list())
  140.         goto failure;
  141.     
  142.     theZone = GetZone();
  143.     SetZone (SystemZone());
  144.  
  145.     HLockHi (initCode);
  146.     
  147.     /* Install the InitGrafPatch */
  148.     
  149.     gSaveInitGraf = (InitGrafUPP) ApplyTrapPatch (_InitGraf, NewInitGrafProc (Patched_InitGraf));
  150.     
  151. failure:;
  152. }
  153.     
  154.